load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_cc//cc:defs.bzl", "cc_test")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

cmake(
    name = "liba",
    generate_args = ["-GNinja"],
    # Demonstrate non-alphanumeric name
    lib_name = "liba++",
    lib_source = "//cmake_synthetic/liba:a_srcs",
    postfix_script = "cp -p $$INSTALLDIR$$/lib/liba.a $$INSTALLDIR$$/lib/liba++.a",
)

cmake(
    name = "libb",
    generate_args = ["-GNinja"],
    lib_source = "//cmake_synthetic/libb:b_srcs",
    deps = [":liba"],
)

cmake(
    name = "lib_with_duplicate_transitive_bazel_deps",
    cache_entries = {
        "LIBA_DIR": "$$EXT_BUILD_DEPS$$",
        "LIBB_DIR": "$$EXT_BUILD_DEPS$$",
    },
    generate_args = ["-GNinja"],
    lib_name = "libc",
    lib_source = "//cmake_synthetic/libc:c_srcs",
    deps = [
        "//cmake_synthetic/liba:lib_a_bazel",
        "//cmake_synthetic/libb:lib_b_bazel",
    ],
)

build_test(
    name = "test_bazel_transitive_deps",
    targets = [":lib_with_duplicate_transitive_bazel_deps"],
)

cc_test(
    name = "test_libs",
    srcs = [
        "test_libb.cpp",
    ],
    deps = [
        # liba should come from transitive dependencies
        ":libb",
    ],
)
